package com.dnjn.mes.biz.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dnjn.mes.biz.domain.*;
import com.dnjn.mes.biz.domain.vo.RecordExceptPlanId;
import com.dnjn.mes.biz.enums.ExceptStatus;
import com.dnjn.mes.biz.mapper.MesRecordExceptMapper;
import com.dnjn.mes.biz.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Objects;

import static com.dnjn.mes.common.utils.SecurityUtils.getUsername;

/**
 * <p>
 * 异常报警表，正确状态不写入 服务实现类
 * </p>
 *
 * @author Amber
 * @since 2023-07-26
 */
@Service
public class MesRecordExceptServiceImpl extends ServiceImpl<MesRecordExceptMapper, MesRecordExcept> implements IMesRecordExceptService {

    @Autowired
    private IMesMessageService messageService;

    @Autowired
    private IMesExceptService exceptService;

    @Autowired
    private IMesResolvePlanService planService;

    @Autowired
    private IMesRecordExceptXrefService exceptXrefService;

    @Override
    public Integer countRecords(Long equipmentId) {
        return baseMapper.countRecords(equipmentId);
    }

    @Override
    @Transactional
    public int updateRecordStatus(RecordExceptPlanId vo, long equipmentId) {
        Long recordExceptId = vo.getRecordExceptId();
        Long exceptId = vo.getExceptId();
        Long[] planIds = vo.getPlanIds();
        //更新MesRecordExcept
        MesRecordExcept domain = new MesRecordExcept();
        domain.setRecordExceptId(recordExceptId);
        //domain.setPlanId(planId);
        domain.setExceptId(exceptId);
        domain.setExceptStatus(ExceptStatus.DONE);
        //更新解决时间
        domain.setResolvedTime(LocalDateTime.now());
        domain.setUpdateBy(getUsername());//?更新故障处理人？
        //关联方案mes_record_except_xref
        for (Long planId : planIds) {
            Long count = exceptXrefService.lambdaQuery().eq(MesRecordExceptXref::getPlanId, planId).eq(MesRecordExceptXref::getRecordExceptId, recordExceptId).eq(MesRecordExceptXref::getEquipmentId, equipmentId).count();
            if(count > 0){
                continue;
            }
            MesRecordExceptXref exceptXref = new MesRecordExceptXref()
                    .setRecordExceptId(recordExceptId)
                    .setPlanId(planId)
                    .setEquipmentId(equipmentId);
            exceptXrefService.save(exceptXref);
        }
        //更新MesMessage
        MesMessage message = new MesMessage();
        message.setExceptId(domain.getExceptId());
        messageService.updateStatus(message);
        return baseMapper.updateRecordStatus(domain);
    }

    @Override
    public List<MesRecordExcept> getRecordExceptList(Long equipmentId) {
        List<MesRecordExcept> exceptList = baseMapper.getRecordExceptList(equipmentId);
        for (MesRecordExcept recordExcept : exceptList) {
            if (ExceptStatus.DONE.equals(recordExcept.getExceptStatus()) && !Objects.isNull(recordExcept.getPlanId())) {//已处理得查询关联的一条方案
                MesResolvePlan plan = planService.getPlan(recordExcept.getPlanId());
                if (plan != null){
                    recordExcept.setDescription(plan.getDescription());
                }
            }
            if (ExceptStatus.TODO.equals(recordExcept.getExceptStatus()) && !Objects.isNull(recordExcept.getExceptId())) {//未处理得通过exceptId关联limit查询一条方案
                String description = exceptService.getDescriptionByExceptId(recordExcept.getExceptId());
                recordExcept.setDescription(description);
            }
        }
        return exceptList;
    }

    @Override
    public List<MesRecordExcept> countRecordExcepts(String startTime, String endTime,long equipmentId) {
        return baseMapper.countRecordExcepts(startTime,endTime,equipmentId);
    }

    @Override
    public List<MesRecordExcept> faultAlarmRecord(long equipmentId, String searchKey) {
        return baseMapper.faultAlarmRecord(equipmentId,searchKey);
    }

    @Override
    public List<MesRecordExcept> countMaintenanceTime(String startTime, String endTime, long equipmentId) {
        return baseMapper.countMaintenanceTime(startTime,endTime,equipmentId);
    }

    @Override
    public List<MesRecordExcept> recordExceptCit(String startTime, String endTime, long equipmentId) {
        return baseMapper.recordExceptCit(startTime,endTime,equipmentId);
    }

}
